#version 450

layout(location=0) in vec3 v_Normal;
layout(location=1) in vec3 v_Color;
layout(location=2) in vec2 v_Texcoord;
layout(location=3) in vec3 v_LightVec;
layout(location=4) in vec3 v_ViewVec;

layout(set=0, binding=1) uniform sampler2D u_texure;

layout(constant_id=0) const int LIGHTING_MODE = 0;
layout(constant_id=1) const float PARAM_TOON_DESATURATION = 0.0f;

layout(location=0) out vec4 fragColor;

void main() {
    switch (LIGHTING_MODE) {
        case 0:
        {
            vec3 color = v_Color;
            vec3 N = normalize(v_Normal);
            vec3 L = normalize(v_LightVec);
            vec3 V = normalize(v_ViewVec);
            vec3 R = reflect(-L, N);
            vec3 ambient = color * 0.25;
            vec3 diffuse = max(dot(L, N), 0.0) * color;
            vec3 specular = pow(max(dot(R, V), 0.0), 32.0) * vec3(0.75);
            fragColor = vec4(ambient + diffuse * 1.75f + specular, 1.0);
            break;
        }
        case 1:
        {
            vec3 N = normalize(v_Normal);
            vec3 L = normalize(v_LightVec);
            float intensity = dot(N, L);
            vec3 color;
            if (intensity > 0.98) {
                color = v_Color * 1.5;
            } else if (intensity > 0.9) {
                color = v_Color * 1.0;
            } else if (intensity > 0.5) {
                color = v_Color * 0.6;
            } else if (intensity > 0.25) {
                color = v_Color * 0.4;
            } else {
                color = v_Color * 0.2;
            }
            color = vec3(mix(color, vec3(dot(vec3(0.2126, 0.7152, 0.0722), color)), PARAM_TOON_DESATURATION));
            fragColor.rgb = color;
            break;
        }
        case 2:
        {
            vec4 color = texture(u_texure, v_Texcoord).rrra;
            vec3 N = normalize(v_Normal);
            vec3 L = normalize(v_LightVec);
            vec3 V = normalize(v_ViewVec);
            vec3 R = reflect(-L, N);
            vec3 ambient = color.rgb * 0.25 * v_Color;
            vec3 diffuse = max(dot(L, N), 0.0) * color.rgb;
            float specular = pow(max(dot(R, V), 0.0), 32.0) * color.a;
            fragColor = vec4(ambient + diffuse + vec3(specular), 1.0);
            break;
        }
    }
}